package com.beiding.workordersystem.orders.generalorder.controller;

import com.beiding.workordersystem.common.dao.MPage;
import com.beiding.workordersystem.common.standard.E;
import com.beiding.workordersystem.common.standard.R;
import com.beiding.workordersystem.orders.generalorder.entity.GeneralOrder;
import com.beiding.workordersystem.orders.generalorder.entity.OrderProcess;
import com.beiding.workordersystem.orders.generalorder.query.OrderQuery;
import com.beiding.workordersystem.orders.generalorder.vo.OrderProcessDesignateVo;
import com.beiding.workordersystem.orders.generalorder.vo.OrderProcessVo;
import com.beiding.workordersystem.orders.generalorder.vo.RefuseVo;
import com.beiding.workordersystem.orders.generalorder.service.GeneralOrderService;
import com.beiding.workordersystem.orders.generalorder.service.OrderProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/generalOrder/auditor")
public class AuditorOrderController {

    @Autowired
    private GeneralOrderService generalOrderService;

    @Autowired
    private OrderProcessService orderProcessService;

    @PostMapping("/delete")
    @PreAuthorize("hasAnyAuthority('generalOrder_delete')")
    R delete(@RequestBody String[] ids) {
        generalOrderService.delete(ids);
        return R.ok();
    }

    //TODO  退单操作
    @PostMapping("/chargeback")
    R chargeback(@RequestBody String[] ids) {
        generalOrderService.chargeback(ids);
        return R.ok();
    }

    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('generalOrder_get')")
    R page(MPage page, OrderQuery query) {
        return R.ok(generalOrderService.pageAuditor(page, query));
    }

    @PostMapping("/pass")
    @PreAuthorize("hasAnyAuthority('generalOrder_pass')")
    R pass(@RequestBody String[] ids) {
        generalOrderService.pass(ids);
        return R.ok();
    }

    @PostMapping("/refuse")
    @PreAuthorize("hasAnyAuthority('generalOrder_refuse')")
    R pass(@RequestBody @Validated RefuseVo refuseVo) {
        generalOrderService.refuse(refuseVo.getId(), refuseVo.getReason());
        return R.ok();
    }

    @GetMapping("/close")
    @PreAuthorize("hasAnyAuthority('generalOrder_close')")
    R close(@RequestParam String id) {
        generalOrderService.close(id);
        return R.ok();
    }

    @PostMapping("/designate")
    @PreAuthorize("hasAnyAuthority('generalOrder_orderProcess_designate')")
    R designateOrderProcess(@RequestBody @Validated OrderProcessDesignateVo vo) {
        E.isFalse(vo.getProcesses() == null || vo.getProcesses().size() == 0, "不存在有效工序");
        if (vo.getType() == 0) {
            for (OrderProcessVo process : vo.getProcesses()) {
                process.setPrincipalId(null);
            }
        }
        List<OrderProcess> orderProcesses = new ArrayList<>();
        Set<Integer> ors = new HashSet<>();
        for (OrderProcessVo process : vo.getProcesses()) {
            OrderProcess orderProcess = new OrderProcess();
            BeanUtils.copyProperties(process, orderProcess);
            orderProcess.setOrderId(vo.getOrderId());
            orderProcesses.add(orderProcess);

            //编号检查
            E.notNull(orderProcess.getSort(), "未给定编号");
            E.isFalse(ors.contains(orderProcess.getSort()), "编号重复");
            ors.add(orderProcess.getSort());
        }
        orderProcessService.designate(vo.getOrderId(), vo.getType(), orderProcesses);
        return R.ok();
    }


    @PostMapping("/publish")
    @PreAuthorize("hasAnyAuthority('generalOrder_orderProcess_designate')")
    R publish(@RequestBody String[] ids) {
        orderProcessService.publish(ids);
        return R.ok();
    }

    //移除工单负责人使之可重新抢单
    @GetMapping("/removePrincipal")
    R removePrincipal(@RequestParam String orderId) {
        generalOrderService.removePrincipal(orderId);
        return R.ok();
    }


    @GetMapping("/activeNextProcess")
    R activeNext(@RequestParam String id) {
        orderProcessService.activeNextProcess(id);
        return R.ok();
    }


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

}
